package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.dao.jdbc.TeachingClassesTemplate;
import com.wuseguang.springcloud.entity.TeachingClasses;
import com.wuseguang.springcloud.service.TeachingClassesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 管理班级和任教班级
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class TeachingClassesServiceImpl implements TeachingClassesService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TeachingClassesTemplate teachingClassesTemplate;

    @Autowired
    private ClassCardFieldService service;

    @Override
    public Map<String, List<TeachingClasses>> findAll(String schoolId, String teacherId) {
        String sql = "SELECT r.gradeid,r.classid, r.classname,r.class_icon,t.name teachername,t.id teacherid,(SELECT COUNT(1) FROM student u WHERE u.class_id=r.classid) COUNT FROM " +
                "(SELECT s.name classname ,g.id gradeid,s.class_icon,s.class_teacher_id,s.school_id,s.id classid FROM grade g, classes  s " +
                " WHERE g.id=s.grade_id AND g.school_id= s.school_id AND s.graduation=0 AND s.school_id=?  ) r " +
                "LEFT JOIN teacher t ON r.class_teacher_id=t.id  AND r.school_id=t.school_id";//我是班主任的班级

        String sql1 = "SELECT DISTINCT e.name SUBJECT,t.id teachingteacherid,t.name teachingteachername,r.pid classid,a.name classname,e.id courseid  " +
                "   FROM classes a ,course e,teacher t,teacher_role r,roles s,grade g WHERE " +
                "    a.graduation=0 AND t.id=r.teacher_id AND e.id=s.course_id AND s.id=r.role_id AND g.id=a.grade_id  AND a.id=r.pid " +
                "    AND t.id=? AND s.type=1 AND r.type=3 ";//我是任教的班级
        List<TeachingClasses> list = (List<TeachingClasses>) teachingClassesTemplate.queryList(sql, new BeanPropertyRowMapper<>(TeachingClasses.class), new Object[]{schoolId});
        //任教的信息
        List<TeachingClasses> list1 = (List<TeachingClasses>) teachingClassesTemplate.queryList(sql1, new BeanPropertyRowMapper<>(TeachingClasses.class), new Object[]{teacherId});
        return distinguishingClasses1(list, list1, teacherId);
    }


    /**
     * @描述 我是班主任的班级和自己的任教班级信息整合一起
     * @创建人 zm
     * @创建时间 2019/11/28
     */
    public Map<String, List<TeachingClasses>> distinguishingClasses1(List<TeachingClasses> list, List<TeachingClasses> list1, String teacherId) {
        logger.info("distinguishingClasses1==>list={},list1={}", list, list1);
        Map<String, List<TeachingClasses>> map = new HashMap<String, List<TeachingClasses>>();
        List<TeachingClasses> list3 = new ArrayList<TeachingClasses>(); //任教班级
        List<TeachingClasses> list4 = new ArrayList<TeachingClasses>();
        List<TeachingClasses> list5 = new ArrayList<TeachingClasses>();
        List<TeachingClasses> list2 = new ArrayList<TeachingClasses>();//我是班主任的班级
        if (list != null && list.size() > 0 && list1 != null && list1.size() > 0) { //将任教信息插入到管理的班级。
            ArrayList<String> strings = new ArrayList<>();
            for (TeachingClasses teachingClasses : list1) {
                strings.add(teachingClasses.getClassid());
            }
            for (TeachingClasses teachingClasses : list) {
                String classid = teachingClasses.getClassid();
                String teacherid = teachingClasses.getTeacherid();
                if (classid != null) {
                    logger.info("classid={}", classid);
                    if (strings.contains(classid)) {
                        //是班主任或者任教老师
                        for (TeachingClasses teachingClasses1 : list1) {
                            logger.info("teachingClasses1.getClassid()={}", teachingClasses1.getClassid());
                            if (classid.equals(teachingClasses1.getClassid())) {
                                String subject = teachingClasses1.getSubject(); //任教科目
                                teachingClasses1.setClassid(classid);
                                teachingClasses1.setClass_icon(teachingClasses.getClass_icon());
                                teachingClasses1.setTeacherid(teachingClasses.getTeacherid());
                                teachingClasses1.setTeachername(teachingClasses.getTeachername());
                                teachingClasses1.setCount(teachingClasses.getCount());
                                teachingClasses1.setGradeid(teachingClasses.getGradeid());
                                teachingClasses1.setGradename(teachingClasses.getGradename());
                                logger.info("subject={},Teachingteacherid()={},Teachingteachername={}", subject, teachingClasses1.getTeachingteacherid(), teachingClasses1.getTeachingteachername());
                                if (teacherid != null && teacherid.equals(teachingClasses1.getTeachingteacherid())) {
                                    logger.info("即是任教又是班主任的班级，添加到管理班级里");
                                    list4.add(teachingClasses);//携带任教信息
                                }
                                logger.info("任教班级的信息,teachingClasses={}", teachingClasses.toString());
                                list3.add(teachingClasses1);
                            }
                        }
                    } else {
                        logger.info("我是班主任的班级/没有任教的老师");
                        if (teacherId.equals(teacherid)) {//我是班主任的班级信息
                            list2.add(teachingClasses);
                        }
                    }
                }
            }
            list4.addAll(list2);
            if (list4 != null && list4.size() > 0) {
                list5.add(list4.get(0));
                map.put("myclass", list5);
            } else {
                map.put("myclass", list4);
            }
            logger.info("teachingclasses,list3={}", list3.toString());
            map.put("teachingclasses", list3);
        } else {
            if (list != null && list.size() > 0) {
                for (TeachingClasses teachingClasses : list) {
                    String teacherid = teachingClasses.getTeacherid();
                    if (!StringUtils.isEmpty(teacherId) && teacherId.equals(teacherid)) {
                        list4.add(teachingClasses);
                    }
                }
            }
            if (list4 != null && list4.size() > 0) {
                list5.add(list4.get(0));
                map.put("myclass", list5);
            } else {
                map.put("myclass", list4);
            }
            map.put("teachingclasses", list3);
        }
        return map;
    }


    /**
     * @描述 查询班级信息
     * @创建人 zm
     * @创建时间 2019/12/15
     */
    @Override
    public Map<String, String> onefindClass(String schoolId, String classid) {
        String sql = "SELECT s.id,class_icon,s.name,s.slogan,g.name gradename FROM classes s,grade g WHERE s.grade_id=g.id AND s.school_id=? AND s.id=?";
        return teachingClassesTemplate.queryclass(sql, schoolId, classid);
    }

    /**
     * @描述 修改班级信息
     * @创建人 zm
     * @创建时间 2019/12/15
     */
    @Override
    public int updateClass(String schoolId, String classicon, String classname, String slogan, String classid) {
        String sql = "UPDATE classes SET class_icon=?,name=?,slogan=? WHERE id=?";
        int i = teachingClassesTemplate.updateByParameter(sql, classicon, classname, slogan, classid);
        if (i > 0) {
            service.classesAndCards(classid, "classflag", "1");
        }
        return i;
    }
}
